package com.superlistaportable.sales;

import com.superlistaportable.productList.ProductListActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class SalesController {
	private final SalesView view;
	private final SalesModel model;
	private final SalesActivity activity;
	private final SalesContinueDialog conFailedDialog;
	private final Intent intent;
	
	public SalesController(SalesModel model, SalesView view, SalesActivity activity, Intent i) {
		this.view = view;
		this.model = model;
		this.activity = activity;
		this.intent = i;
		this.conFailedDialog = new SalesContinueDialog(this);
		if(model.getNewStatuses().isEmpty()){
			//show connection failed dialog
			conFailedDialog.show();
		}
	}
	
	public void setListeners() {

		view.getUpdateSalesButton().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Toast.makeText(activity.getApplicationContext(), "Actualizando...",Toast.LENGTH_SHORT).show();
				view.updateListView();
				 if(model.getNewStatuses().isEmpty()){
			        //show connection failed dialog
					 conFailedDialog.show();
			        }
			}
		});
	}
	public SalesActivity getActivity(){
		return this.activity;
	}

	public void returnToProductList() {
		String selectedSupermarket = "";
		Intent intentProdList = new Intent(activity, ProductListActivity.class);
		Bundle extras = intent.getExtras();
		if (extras != null) {
			selectedSupermarket = extras.getString("selectedSupermarket");
        }
		intentProdList.putExtra("selectedSupermarket",selectedSupermarket);
    	activity.startActivity(intentProdList);
	}
}
